<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #wrap{
            width:300px;
            height:300px;
            margin: 200px auto;
            border: 1px solid black;
            padding-left: 40px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <p><input type="text" name="" id="username" placeholder="请输入用户名"></p> 
        <p><input type="password" name="" id="password" placeholder="请输入密码"></p>
        <p>
            <input type="checkbox" name="" id="mdl">
            30天内免登陆
        </p>
        <button id="login">登陆</button>
    </div>
    <!-- 存放地址的变量 -->
    <script src="url.js"></script>
    <script src="jquery.min.js"></script>
    <script>
        $("#login").click(function(){
            let username = $("#username").val()
            let password = $("#password").val()
            // console.log(password)
            // 如果用户名和密码都输入了，向后端发送ajax请求
            if(username && password){
                $.ajax({
                    url:`${address}/login`,
                    data:{
                        username:username,
                        password:password
                    },
                    success(data){
                        if(data==="0"){
                            alert("用户名或密码错误")
                        }
                        else{
                            // alert("3")
                            // 30天免登陆判断
                            if($("#mdl")[0].checked){
                                // 当前时间
                                let date = new Date()
                                // 到期时间
                                date.setDate(date.getDate()+30)
                                // console.log(data.getDate())
                                document.cookie=`information=${data};expires=${date}`
                            }

                            sessionStorage.setItem("information",data)
                            location.href = `${address}/students.html`
                        }
                    }
                })
            }
        })
    </script>
</body>
</html>